/*==============================================================================
FILE NAME: Figure_A3.do
CREATED: 12 June 2025
==============================================================================*/

**Figure A3

/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/


use "$processed_data/Air_Panel.dta", clear

// Drop if there was never an air investigation
drop if never_air_inv == 1

// Create panel time variable
egen t = group(year month)
xtset RN_id t

// Generate variables for outcomes
forv h = 0/12 {
	gen p_air_inv_`h' = f`h'.p_air_inv - l1.p_air_inv
}

// Generate variables for placebo
forv h = 2/12 {
	gen p_air_inv_neg`h' = l`h'.p_air_inv - l1.p_air_inv
}

// Create a unit-year fixed effect variable
egen RN_year = group(RN year)

tempfile coeffs

// Create an empty dataset to store coefficients
save `coeffs', emptyok replace

foreach n of numlist 1/16 {
    preserve
    
    keep if region_id == `n'

    // Initialize the data structure with the baseline and future time horizons
    gen Years = _n - 2 if _n <= 14  // This creates Years from -1 to 12
    gen coef = .
    gen region = `n'

    // Set baseline coefficient (t = -1)
    replace coef = 0 if Years == -1

    // Loop over time horizons 0 to 12
    foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
        reghdfe p_air_inv_`h' p_air_incident, absorb(RN_year t) cluster(RN_id)

        // Replace the coefficient for the corresponding time horizon
        replace coef = _b[p_air_incident] if Years == `h'
    }

    // Append the entire block of data for this region to the master file
    append using `coeffs'
    save `coeffs', replace

    restore
}

// Load the saved coefficients
use `coeffs', clear
drop if Years > 12
graph set window fontface "Times New Roman"
twoway (line coef Years if region == 1, lcolor(blue) lpattern(solid) legend(label(1 "Region 1"))) ///
       (line coef Years if region == 2, lcolor(red) lpattern(solid) legend(label(2 "Region 2"))) ///
       (line coef Years if region == 3, lcolor(green) lpattern(solid) legend(label(3 "Region 3"))) ///
       (line coef Years if region == 4, lcolor(orange) lpattern(solid) legend(label(4 "Region 4"))) ///
       (line coef Years if region == 5, lcolor(purple) lpattern(solid) legend(label(5 "Region 5"))) ///
       (line coef Years if region == 6, lcolor(black) lpattern(solid) legend(label(6 "Region 6"))) ///
       (line coef Years if region == 7, lcolor(yellow) lpattern(solid) legend(label(7 "Region 7"))) ///
       (line coef Years if region == 8, lcolor(pink) lpattern(solid) legend(label(8 "Region 8"))) ///
       (line coef Years if region == 9, lcolor(brown) lpattern(solid) legend(label(9 "Region 9"))) ///
       (line coef Years if region == 10, lcolor(teal) lpattern(solid) legend(label(10 "Region 10"))) ///
       (line coef Years if region == 11, lcolor(olive) lpattern(solid) legend(label(11 "Region 11"))) ///
       (line coef Years if region == 12, lcolor(cyan) lpattern(solid) legend(label(12 "Region 12"))) ///
       (line coef Years if region == 13, lcolor(magenta) lpattern(solid) legend(label(13 "Region 13"))) ///
       (line coef Years if region == 14, lcolor(navy) lpattern(solid) legend(label(14 "Region 14"))) ///
       (line coef Years if region == 15, lcolor(gold) lpattern(solid) legend(label(15 "Region 15"))) ///
       (line coef Years if region == 16, lcolor(lime) lpattern(solid) legend(label(16 "Region 16"))), ///
       xtitle("Event Month") ytitle("{&Delta} P(Air Investigations)") ///
       xlabel(-1(1)12) legend(pos(6) col(4))

// Save figures	   
graph export "$figures/Figure_A3.pdf", replace

//Save point estimates
export delimited region Years coef using "$point_estimates/Point_Estimates_Figure_A3.csv", replace
